#!/bin/bash
function parse_yaml {
   local prefix=$2
   local s='[[:space:]]*' w='[a-zA-Z0-9_]*' fs=$(echo @|tr @ '\034')
   sed -ne "s|^\($s\):|\1|" \
        -e "s|^\($s\)\($w\)$s:$s[\"']\(.*\)[\"']$s\$|\1$fs\2$fs\3|p" \
        -e "s|^\($s\)\($w\)$s:$s\(.*\)$s\$|\1$fs\2$fs\3|p"  $1 |
   awk -F$fs '{
      indent = length($1)/2;
      vname[indent] = $2;
      for (i in vname) {if (i > indent) {delete vname[i]}}
      if (length($3) > 0) {
         vn=""; for (i=0; i<indent; i++) {vn=(vn)(vname[i])("_")}
         printf("%s%s%s=\"%s\"\n", "'$prefix'",vn, $2, $3);
      }
   }'
}
export HOMEDIR=$(dirname $(dirname $(readlink -f "${BASH_SOURCE[0]}")))

eval $(parse_yaml "$HOMEDIR/config/install-config.yaml" "ptk_")

export GSQLEXEC="gsql -r -d $ptk_global_biz_dbname "
export OSEXEC="sudo -u $ptk_global_user -i "
export LD_LIBRARY_PATH=$HOMEDIR/workspace/oracle-client:$LD_LIBRARY_PATH
export LANG=zh_CN.UTF-8

arch_result=$(arch)

if [ "$arch_result" == "x86_64" ]; then
    eval $(parse_yaml "$HOMEDIR/config/software-config-x86_64.yaml" "pkg_")
elif [ "$arch_result" == "aarch64" ]; then
    eval $(parse_yaml "$HOMEDIR/config/software-config-arm64.yaml" "pkg_")
else
    echo "错误：未知的架构类型"
    exit 1
fi

export server_version=`echo $pkg_server | grep -oP 'MogDB-\K[0-9\.]+(?=-Kylin)'`

if [[ "$server_version" != "5.2.0" && "$server_version" != "5.2.1" ]]; then
    echo "错误: 本脚本支持的MogDB版本为 5.2.0 以及 5.2.1 ，暂不支持 MogDB $server_version 版本的安装"
    exit 1
fi